import 'package:flutter_deer/generated/json/base/json_convert_content.dart';


import 'package:flutter_deer/res/constant.dart';


import 'package:flutter_deer/generated/json/base/json_field.dart';


import 'package:flutter_deer/generated/json/base_api_data_entity.g.dart';


import 'package:flutter_deer/generated/json/file_upload_entity.g.dart';


class BaseEntity<T> {

  BaseEntity(this.code, this.message, this.data);


  BaseEntity.fromJson(Map<String, dynamic> json) {

    code = json[Constant.code] as int?;


    message = json[Constant.message] as String?;


    if (json.containsKey(Constant.data)) {

      data = _generateOBJ<T>(json[Constant.data] as Object?);

    }

  }


  int? code;


  String? message;


  T? data;


  T? _generateOBJ<O>(Object? json) {

    if (json == null) {

      return null;

    }


    if (T.toString() == 'String') {

      return json.toString() as T;

    } else if (T.toString() == 'Map<dynamic, dynamic>') {

      return json as T;

    } else {

      /// List类型数据由fromJsonAsT判断处理


      return JsonConvert.fromJsonAsT<T>(json);

    }

  }

}


class OperatorBaseEntity {

  OperatorBaseEntity(this.code, this.message, this.data);


  OperatorBaseEntity.fromJson(Map<String, dynamic> json) {

    code = json[Constant.code] as int?;


    message = json[Constant.message] as String?;


    data = json[Constant.data] as bool?;

  }


  int? code;


  String? message;


  bool? data;

}


class StringBaseEntity {

  StringBaseEntity(this.code, this.message, this.data);


  StringBaseEntity.fromJson(Map<String, dynamic> json) {

    code = json[Constant.code] as int?;


    message = json[Constant.message] as String?;


    data = json[Constant.data] as String?;

  }


  int? code;


  String? message;


  String? data;

}


class DoubleBaseEntity {

  DoubleBaseEntity(this.code, this.message, this.data);


  DoubleBaseEntity.fromJson(Map<String, dynamic> json) {

    code = json[Constant.code] as int?;


    message = json[Constant.message] as String?;


    data = json[Constant.data] as double?;

  }


  int? code;


  String? message;


  double? data;

}


class BaseApiDataEntity<T> {

  BaseApiDataEntity(

      {this.ifQuery,

      this.sql,

      this.time,

      this.success,

      this.errorMsg,

      this.affectedRows,

      this.columns,

      this.rowData});


  BaseApiDataEntity.fromJson(Map<String, dynamic> json) {

    ifQuery = json['ifQuery'] as bool?;


    sql = json['sql'] as String?;


    time = json['time'] as String?;


    success = json['success'] as bool?;


    errorMsg = json['errorMsg'] as String?;


    affectedRows = json['affectedRows'] as int?;


    columns = JsonConvert.fromJsonAsT<List<String>>(json['columns']);


    if (json.containsKey('rowData')) {

      rowData = _generateOBJ<T>(json[Constant.data] as Object?);

    }

  }


  bool? ifQuery;


  String? sql;


  String? time;


  bool? success;


  String? errorMsg;


  int? affectedRows;


  List<String>? columns;


  T? rowData;


  T? _generateOBJ<O>(Object? json) {

    if (json == null) {

      return null;

    }


    if (T.toString() == 'String') {

      return json.toString() as T;

    } else if (T.toString() == 'Map<dynamic, dynamic>') {

      return json as T;

    } else {

      /// List类型数据由fromJsonAsT判断处理


      return JsonConvert.fromJsonAsT<T>(json);

    }

  }

}


@JsonSerializable()

class FileUploadEntity {

  FileUploadEntity(

      {this.bucketName, this.fileName, this.url, this.originName, this.fileId});


  factory FileUploadEntity.fromJson(Map<String, dynamic> json) =>

      $FileUploadEntityFromJson(json);


  Map<String, dynamic> toJson() => $FileUploadEntityToJson(this);


  @JSONField(name: 'bucketName')

  String? bucketName;


  @JSONField(name: 'fileName')

  String? fileName;


  @JSONField(name: 'url')

  String? url;


  @JSONField(name: 'originName')

  String? originName;


  @JSONField(name: 'fileId')

  String? fileId;

}

